package org.dawfinn.droiditarian;

import org.dawfinn.droiditarian.data.DeedsDBAdapter;
import org.dawfinn.droiditarian.view.DeedCursorAdapter;
import org.dawfinn.droiditarian.view.DeedView;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FavoritedDeeds extends Activity {

	private Cursor favoritedDeeds;
	private DeedCursorAdapter adapter;
	private DeedsDBAdapter dbAdapter;
	
	private TextView emptyText;
	
	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.favorited_list);
		

		dbAdapter = new DeedsDBAdapter(this);
		dbAdapter.open();

		favoritedDeeds = dbAdapter.getFavoritedDeedsCursor();
		startManagingCursor(favoritedDeeds);
		adapter = new DeedCursorAdapter(this, favoritedDeeds);

		emptyText = (TextView) findViewById(R.id.emptyText);
		ListView deedsList = (ListView) findViewById(R.id.favoritedList);
		
		deedsList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(FavoritedDeeds.this, ExpandedDeed.class);
				intent.putExtra("deedKey", ((DeedView) view).getDeed().getId());
				startActivity(intent);
			}
		});
		deedsList.setAdapter(adapter);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		favoritedDeeds.requery();
		if (!favoritedDeeds.isAfterLast()) {
			emptyText.setVisibility(View.GONE);
		} else {
			emptyText.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		stopManagingCursor(favoritedDeeds);
		dbAdapter.close();
	}
}
